<template>
	<view class="zx-user">
		<!-- 用户登录 -->
		<view class="user-login">
			<view class="user-info">
				<image class="bg-set" :src="backgroundUrl"></image>
				<view class="user-info__avatar">
					<image v-if="Object.keys(userInfo).length>0" :src="userInfo.avatarUrl"></image>
					<image v-else src="../../../static/img/avatar.png"></image>
				</view>
				<view class="user-info__info">
					<button v-if="userInfo.nickName==undefined" plain="true" type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">点击获取头像</button>
					<button v-else plain="true" type="primary">{{ userInfo.nickName }}</button>
				</view>
				<view class="user-info__code" @click="navTo('/pages/point/signin')">
					<text class="i-tabbar-icons icon" style="color: #fff;">&#xe618;</text>
					<view class="i-tabbar-txt">签到</view>
				</view>
			</view>
		</view>
		<view class="user-sta" type="v-stats">
			<view class="user-sta__item"  @click="navTo('/pages/point/point-center')">
				<view class="user-sta__item__value">
					{{ totalPoint }}
					<!---->
					<view class="user-sta__item__title">积分</view>
				</view>
			</view>
			<view class="user-sta__item">
				<view class="user-sta__item__value">
					0
					<!---->
					<view class="user-sta__item__title">优惠券</view>
				</view>
			</view>
			<view class="user-sta__item" style="display: none;">
				<view class="user-sta__item__value">
					0
					<!---->
					<view class="user-sta__item__title">卡</view>
				</view>
			</view>
		</view>
		<!-- 订单中心 -->
		<view class="user-order">
			<view class="all-order" @click="navTo('/pages/order/order-list?state=0')">
				<view class="title"><text>我的订单</text></view>
				<view class="value"><text>查看全部订单</text></view>
				<text class="more yticon icon-you"></text>
			</view>
			<view class="line"></view>
			<view class="order-tab">
				<view class="item" @click="navTo('/pages/order/order-list?state=1')">
					<view class="item__icon">
						<view class="item-icon">
							<view class="item-image">
								<text class="tabbar-icons icon">&#xe63d;</text>
							</view>
							<view class="item__info" v-if="orderOneNum>0">
								{{orderOneNum}}
							</view>
						</view>
					</view>
					<view class="item__text">
						待付款
					</view>
				</view>
				<view class="item" @click="navTo('/pages/order/order-list?state=2')">
					<view class="item__icon">
						<view class="item-icon">
							<view class="item-image">
								<text class="tabbar-icons icon">&#xe642;</text>
							</view>
							<view class="item__info" v-if="orderTwoNum>0">
								{{orderTwoNum}}
							</view>
						</view>
					</view>
					<view class="item__text">
						待发货
					</view>
				</view>
				<view class="item" @click="navTo('/pages/order/order-list?state=3')">
					<view class="item__icon">
						<view class="item-icon">
							<view class="item-image">
								<text class="tabbar-icons icon">&#xe63f;</text>
							</view>
							<view class="item__info" v-if="orderThreeNum>0">
								{{orderThreeNum}}
							</view>
						</view>
					</view>
					<view class="item__text">
						待收货
					</view>
				</view>
				<view class="item" @click="navTo('/pages/order/order-list?state=4')">
					<view class="item__icon">
						<view class="item-icon">
							<view class="item-image">
								<text class="tabbar-icons icon">&#xe63e;</text>
							</view>
							<view class="item__info" v-if="orderFourNum>0">
								{{orderFourNum}}
							</view>
						</view>
					</view>
					<view class="item__text">
						待提货
					</view>
				</view>
			</view>
		</view>
		<!-- 功能列表 -->
		<view class="user-plugin" style="display: none;">
			<view class="group">
				<view class="group__item" @click="navTo('/pages/admin/menu')">
					<view class="left-icon">
						<text class="icon">&#xe600;</text>
					</view>
					<view class="title"><text>我的售后</text></view>
					<text class="more yticon icon-you"></text>
				</view>
			</view>
		</view>
		<view class="user-plugin">
			<view class="group">
				<view class="group__item" @click="navTo('/pages/address/address-list')">
					<view class="left-icon">
						<text class="icon">&#xe60f;</text>
					</view>
					<view class="title"><text>收货地址</text></view>
					<text class="more yticon icon-you"></text>
				</view>
				<view class="line"></view>
				<view class="group__item" @click="navTo('/pages/user/user-edit')">
					<view class="left-icon">
						<text class="icon">&#xe676;</text>
					</view>
					<view class="title"><text>个人信息</text></view>
					<text class="more yticon icon-you"></text>
				</view>
				<view class="line"></view>
				<view class="group__item" @click="navTo('/pages/setting/setting-edit')">
					<view class="left-icon">
						<text class="icon">&#xe612;</text>
					</view>
					<view class="title"><text>设置</text></view>
					<text class="more yticon icon-you"></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import myCloud from '@/common/util/cloud.js';
	import myConst from '@/common/util/const.js';
	import util from '@/common/util/util.js';
	import { getDataOne } from '@/common/api/order.js'
	import { wxLogin,getUserInfo,updateUser } from '@/common/api/user.js'
	import {
		getPointDataOne
	} from '@/common/api/point.js'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {},
		data() {
			return {
				orderOneNum: 0,
				orderTwoNum: 0,
				orderThreeNum: 0,
				orderFourNum: 0,
				totalPoint: 0,
				backgroundUrl: '/static/img/avatar-back.jpeg',
			}
		},
		created() {
			if (this.hasLogin){
				this.getData()
				this.initPointData()
			}
				
		},
		computed: {
			...mapState(['hasLogin', 'userName', 'userId', 'userInfo'])
		},
		methods: {
			...mapMutations(['login', 'setUserInfo']),
			async getData() {
				const req1 = {
					state: 1,
					userId: this.userId
				}
				const data1 = await getDataOne(req1)
				this.orderOneNum = data1;
				const req2 = {
					state: 2,
					userId: this.userId
				}
				const data2 = await getDataOne(req2)
				this.orderTwoNum = data2;
				const req3 = {
					state: 3,
					userId: this.userId
				}
				const data3 = await getDataOne(req3)
				this.orderThreeNum = data3;
				const req4 = {
					state: 4,
					userId: this.userId
				}
				const data4 = await getDataOne(req4)
				this.orderFourNum = data4;
			},
			async initPointData() {
				const req = {
					userId: this.userId
				}
				const res = await getPointDataOne(req)
				if (res.length > 0) {
					this.totalPoint = res[0].point
				}
			},
			navTo(url) {
				if (!this.hasLogin) {
					this.$api.msg("请先登录系统")
				} else {
					this.$api.navTo(url)
				}
			},
			mpGetUserInfo(result) {
				console.log('mpGetUserInfo', result);
				if(this.userId!=''){
					if (result.detail.errMsg !== 'getUserInfo:ok') {
						uni.showModal({
							title: '获取用户信息失败',
							content: '错误原因' + result.detail.errMsg,
							showCancel: false
						});
						return;
					}
					const data = {
						uid: this.userId,
						userInfo: result.detail.userInfo
					}
					updateUser(data).then(res=>{
						console.log(res)
					})
					uni.setStorageSync('userInfo', result.detail.userInfo)
					this.setUserInfo(result.detail.userInfo)
				}
			},
			oauth(v) {
				var that = this;
				uni.login({
					provider: v,
					success: function(loginRes) {
						const data = {
							code: loginRes.code
						}
						wxLogin(data).then(res=>{
							that.$api.msg(res.msg);
							if (res.code == 0) {
								const data2 = {
									uid: res.uid,
									field: ['mobile']
								}
								getUserInfo(data2).then(res2=>{
									if (res2.code == 0) {
										if (res2.userInfo.mobile == undefined) {
											res.mobile = ''
										} else {
											res.mobile = res2.userInfo.mobile
										}
										uni.setStorageSync('userId', res.uid)
										uni.setStorageSync("userName",res.mobile);
										uni.setStorageSync('uniIdToken', res.token)
										uni.setStorageSync('uni_id_token_expired', res.tokenExpired)
										that.login(res)
									}
								})
							}
						})
					}
				});
			},
		}
	}
</script>

<style lang='scss'>
	.zx-user {
		background-color: $zx-page-background-color;
		padding-bottom: 140rpx;
		margin-bottom: 0rpx;

		.user-login {
			background: #fff;
			margin-bottom: 20rpx;

			.user-info {
				position: relative;
				align-items: center;
				flex-direction: column;
				background-size: 100% 100% !important;
				background-repeat: no-repeat;
				display: flex;
				height: 360rpx;

				.bg-set {
					position: absolute;
					width: 100%;
					height: 360rpx;
				}

				.user-info__avatar {
					background-size: 120rpx 120rpx;
					width: 120rpx;
					height: 120rpx;
					margin-top: 86rpx;
					z-index: 10;

					image {
						width: 120rpx;
						height: 120rpx;
						border: 2rpx solid #fff;
						border-radius: 50%;
					}
				}

				.user-info__info {
					margin-top: 14rpx;
					display: flex;
					flex-direction: column;

					button {
						color: #303133;
						border: 0rpx
					}
				}

				.user-info__code {
					position: absolute;
					top: 20px;
					display: flex;
					align-items: center;
					justify-content: space-around;
					box-sizing: border-box;
					width: auto;
					height: 22px;
					padding: 0 10px;
					color: #fff;
					font-size: 10px;
					background-color: rgba(0, 0, 0, 0.2);
					border-radius: 11px;
					padding-bottom: 2px;
					right: 12px;
					.i-tabbar-icons{
						font-size: 13px;
						margin-right: 2px;
					}
					.i-tabbar-txt{
						font-size: 13px;
					}
				}
			}

		}

		.user-sta {
			width: 94%;
			height: 72px;
			margin: 0 auto 10px;
			border-radius: 8px;
			display: flex;
			flex-direction: row;
			align-items: center;
			background: #fff;

			.user-sta__item {
				position: relative;
				flex: 1;
				line-height: 22px;
				text-align: center;
			}

			.user-sta__item__value {
				position: relative;
				display: inline-block;
				color: #111;
				font-weight: 600;
				font-size: 16px;
			}

			.user-sta__item__title {
				margin-top: 4px;
				color: #999;
				font-size: 12px;
			}
		}

		.user-order {
			width: 94%;
			margin: 0 auto 20rpx;
			background: #fff;
			border-radius: 16rpx;

			.all-order {
				height: 80rpx;
				padding: 20rpx;
				border-radius: 16rpx;
				position: relative;
				display: flex;
				box-sizing: border-box;
				width: 100%;
				padding: 20rpx 32rpx;
				color: #323233;
				font-size: 28rpx;
				line-height: 48rpx;
				background-color: #fff;

				.title {
					color: #111;
					font-weight: 700;
					font-size: 28rpx;
				}

				.value {
					color: #999;
					font-size: 24rpx;
					position: relative;
					overflow: hidden;
					color: #969799;
					text-align: right;
					vertical-align: middle;
					flex: 1;
				}

				.more {
					align-self: baseline;
					font-size: 30rpx;
					color: #909399;
					margin-left: 10rpx;
					margin-top: -1rpx;
				}
			}

			.order-tab {
				position: relative;
				padding: 40rpx 0;
				display: flex;
				box-sizing: initial;
				width: 100%;
				height: 100rpx;
				background-color: #fff;

				.item {
					display: flex;
					flex: 1;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					/* color: #999; */
					font-size: 24rpx;
					line-height: 1;

					.item__icon {
						position: relative;
						margin-bottom: -10rpx;
						font-size: 36rpx;

						.item-image {
							.tabbar-icons {
								font-size: 60rpx;
								color: $zx-font-color-base;
							}
						}

						.item__info {
							position: absolute;
							right: 0;
							top: 0rpx;
							left: 30rpx;
							color: $zx-color-primary;
							font-size: 36rpx;
							font-weight: 500;
							text-align: center;
							box-sizing: border-box;
							padding: 0 6rpx;
							min-width: 34rpx;
							line-height: 28rpx;
							border: 2rpx solid #fff;
							border-radius: 32rpx;
							margin-left: 20rpx;
						}
					}
				}
			}
		}

		.user-plugin {
			padding-bottom: 20rpx;

			.group {
				width: 94%;
				margin: auto;
				border-radius: 16rpx;
				color: #323233;
				font-size: 28rpx;
				line-height: 48rpx;
				background-color: #fff;

				.group__item {
					position: relative;
					display: flex;
					box-sizing: border-box;
					width: 100%;
					padding: 20rpx 32rpx;

					.left-icon {
						margin-right: 20rpx;

						.icon {
							font-size: 36rpx;
							color: $zx-font-color-base;
						}
					}

					.title {
						margin-top: 9rpx;
						color: $zx-font-color-base;
						font-weight: 500;
						font-size: 28rpx;
					}

					.more {
						margin-top: 5rpx;
						position: absolute;
						right: 40rpx;
						color: $zx-font-color-base;
					}
				}
			}
		}

		.line {
			border-bottom: 2rpx solid #ebedf0;
			margin-left: 30rpx;
		}
	}
</style>
